package com.ccp.dev.im.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.im.server.constant.Constants;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ccp.dev.im.dao.ImFriendMessageDao;
import com.ccp.dev.im.model.ImFriendMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;


/**
 * 好友消息 信息操作服务类
 *
 * @author y
 * @date 2020-02-17 11:40:06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ImFriendMessageService extends BaseService<ImFriendMessage> {

    @Resource
    private ImFriendMessageDao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<ImFriendMessage> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (ImFriendMessage data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 根据收件人account查询离线消息
     * @param toId  userid
     * @return  list
     */
    public List<ImFriendMessage> queryOfflineMsg(String toId){
        if(StringUtils.isNotEmpty(toId)){
            Map<String,Object> map = new HashMap(2);
            map.put("toId",toId);
            map.put("isRead", Constants.MsgStatus.IS_READ_N);
            return dao.queryMsgByToIdAndIsRead(map);
        }
        return new ArrayList<>();
    }
    /**
     * 根据id设置消息读取状态
     * @param msgIds  msgId
     */
    public void updateReadStatus(String[] msgIds){
        Map<String,Object> map = new HashMap(2);
        map.put("msgIds",msgIds);
        map.put("isRead", Constants.MsgStatus.IS_READ_Y);
        dao.updateIsReadByMsgId(map);
    };
    /**
     * 根据id设置消息读取状态
     * @param fromId  发消息人
     * @param toId  接收人
     */
    public void updateIsReadByFromIdAndToId(String fromId , String toId){
        Map<String,Object> map = new HashMap(2);
        map.put("fromId",fromId);
        map.put("toId", toId);
        map.put("isRead",  Constants.MsgStatus.IS_READ_Y);
        dao.updateIsReadByFromIdAndToId(map);
    };

    /**
     * 根据接收人和发起人查询数据
     * @param queryFilter  查询条件
     */
    public List<Map<String,Object>> queryMsgByFromIdAndToId (QueryFilter queryFilter){
        return dao.queryMsgByFromIdAndToId(queryFilter);
    }
}
